<?php

namespace app\api\model;

use app\admin\model\vip\Card;
use app\exceptions\ApiException;
use think\Model;

/**
 * Hotel
 */
class Hotel extends Model
{
    // 表名
    protected $name = 'hotel';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'hotelCate',
        'VipCard',
    ];


    public function getHotelCateAttr($value, $row): array
    {
        return [
            'name' => \app\admin\model\hotel\Cate::whereIn('id', $row['hotel_cate_ids'])->column('name'),
        ];
    }

    public function getVipCardAttr($value, $row): array
    {
        return [
            'name' => Card::whereIn('id', $row['vip_card_ids'])->column('name'),
        ];
    }

    public function getHotelCateIdsAttr($value): array
    {
        if ($value === '' || $value === null) {
            return [];
        }
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setHotelCateIdsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getBannerAttr($value): array
    {
        if ($value === '' || $value === null) {
            return [];
        }
        $res = explode(',', $value);
        foreach ($res as &$val){
            $val = full_url($val);
        }
        return  $res;
    }



    public function getHotelDesAttr($value): string
    {
        return !$value ? '' : htmlspecialchars_decode($value);
    }

    public function getTrafficBusAttr($value): string
    {
        return !$value ? '' : htmlspecialchars_decode($value);
    }

    public function getBookNoticeAttr($value): string
    {
        return !$value ? '' : htmlspecialchars_decode($value);
    }

    public function getVipCardIdsAttr($value): array
    {
        if ($value === '' || $value === null) {
            return [];
        }
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }


    public function setVipCardIdsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getCoverAttr($value)
    {
        return full_url($value);
    }


    public function getAllHotel($params)
    {
        $province_name = $params['province_name'] ?? '';
        $city = $params['city'] ?? '';
        $vip_card_id = $params['vip_card_id'] ?? '';
        $search = $params['search'] ?? '';
        $cate_id = $params['cate_id'] ?? '';

        $map[]= ['status', '=', 1];
        if ($province_name) {
            $map[] = ['province_name', '=', $province_name];
        }
        if ($city){
            $other_city = ['北京市','上海市','重庆市','天津市'];
            if(in_array($province_name,$other_city)){
                $map[] = ['city_name', '=', $province_name];
            }else{
                $map[] = ['city_name','=',$city];
            }
        }
        if ($vip_card_id) {
            $map[] = ['vip_card_ids', 'like', "%" . $vip_card_id . "%"];
        }
        if ($cate_id) {
            $map[] = ['hotel_cate_ids', 'like', "%".$cate_id."%"];
        }
        if ($search) {
            $map[] = ['name', 'like', "%" . $search . "%"];
        }



        $list = self::where($map)
            ->field('id as hotel_id,name,cover,address,hotel_cate_ids,vip_card_ids')
            ->paginate(10)->toArray();

        foreach ($list['data'] as &$val) {
            unset($val['hotel_cate_ids'], $val['vip_card_ids']);
        }
        return api_message($list);
    }


    public function hotelDetail($params)
    {
        $hotel_id = $params['hotel_id'] ?? 0;
        $lat = $params['lat'] ?? '';
        $lng = $params['lng'] ?? '';
        $res = self::where(['id' => $hotel_id,'status'=> 1])
            ->field('id as hotel_id,name,banner,phone,address,layout,size,
            hotel_cate_ids,vip_card_ids,city_name,latitude,longitude,hotel_des,traffic_bus,book_notice')
           ->find()->toArray();

        if(!$res) throw new ApiException('数据异常');
        $res['distance'] = (new Scenic())->calculateDistance($lat, $lng, $res['latitude'], $res['longitude']);
        //查询特殊价格
        $hotel_price = (new HotelPrice())
            ->where(['hotel_id' => $hotel_id])
            ->whereTime('date','>=',date("Y-m-d"))
            ->field('date,price')
            ->select()->toArray();
        if($hotel_price){
            foreach ($hotel_price as &$val){
                $time = strtotime($val['date']);
                $val['year'] = date('Y', $time);
                $val['month'] = date('m',$time);
                $val['day'] = date('d',$time);
            }
        }

        $res['hotel_price'] = $hotel_price;

        unset($res['hotel_cate_ids'],$res['vip_card_ids']);
        return api_message($res);
    }
}
